package com.hundsun.yr.universal.p2p.ui.activity;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebChromeClient;
import android.webkit.WebView;

import com.hundsun.mobile.zwgt.R;
import com.hundsun.yr.universal.library.netstatus.NetUtils;
import com.hundsun.yr.universal.p2p.ui.activity.base.BaseMineWebActivity;
import com.hundsun.yr.universal.p2p.utils.Logs;

/**
 *  H5显示页面
 */
public class WebActivity extends BaseMineWebActivity implements OnClickListener{
    /**
     * 支持放大缩小
     */
    public static final String supportZoom = "supportZoom";
    private boolean supportZoomFlag;

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        // TODO Auto-generated method stub
        return true;
    }

    @Override
    protected View getLoadingTargetView() {
        if (mBrowserLayout == null) {
        }
        return mBrowserLayout;
    }


    @Override
    protected void onNetworkConnected(NetUtils.NetType type) {
        mBrowserLayout.refresh();
        toggleHide();
    }

    @Override
    protected void getBundleExtras(Bundle extras) {
        super.getBundleExtras(extras);
        supportZoomFlag = extras.getBoolean(supportZoom);
    }

    @Override
    protected void onNetworkDisConnected() {

        toggleNetworkError(true, new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                if (NetUtils.isNetworkConnected(getBaseContext())) {
                    showToast("请检查网络...");
                } else {
                    mBrowserLayout.refresh();
                    toggleHide();
                }
            }
        });

    }


    @Override
    protected void initViewsAndEvents() {
        super.initViewsAndEvents();
        toolbar_title.setText(mWebTitle);
        mToolBar.setNavigationOnClickListener(this);

        WebChromeClient wvcc = new WebChromeClient() {
            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
                toolbar_title.setText(title);
//                toolbar_title.setText(mWebTitle);
            }
        };
        mBrowserLayout.getWebView().setWebChromeClient(wvcc);

        if (supportZoomFlag) {
            mBrowserLayout.mWebView.getSettings().setSupportZoom(supportZoomFlag);
            mBrowserLayout.mWebView.getSettings().setBuiltInZoomControls(supportZoomFlag);
        }
    }

    protected void initImmersionBar() {
        super.initImmersionBar();
        mImmersionBar.titleBar(R.id.common_toolbar).init();
    }

    @Override
    protected boolean isApplyKitKatTranslucency() {
        // TODO Auto-generated method stub
        return false;
    }

    /**
     * 网页 返回
     */
    public void webBack() {
        if (mBrowserLayout.canGoBack()) {
            mBrowserLayout.goBack();
        } else {
            finish();
        }
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub

        if (keyCode == KeyEvent.KEYCODE_BACK) {

            webBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }


    @Override
    public void onClick(View view) {
        webBack();
    }
}
